package com.itheima.net;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author Azer
 * @LOVE,,,
 * @Date 2021/09/2021/9/4 09:40:03
 * @Version 1.0
 */
//客户端

public class Cilent {

    //TCP协议的客户端对象
    static Socket socket;

    static OutputStream op;
    static InputStream in;

    static byte [] buf;

   static {
        try {
            socket = new Socket("127.0.0.1", 7777);

            //客户端发送消息---输出流
            op = socket.getOutputStream();

            //客户端接收消息---输入流
            in = socket.getInputStream();
            buf=new byte[1024];

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {
        System.out.println("客户端正在启动。。。");
        Thread.sleep(2000);
        System.out.println("客户端启动成功。");

        //展示界面
        showWindows();
    }

    //界面展示
    private static void showWindows() {
        Scanner sc=new Scanner(System.in);
        System.out.println("******欢迎来到天地银行*****");
        System.out.println("请选择您需要进行的操作(1/2):");
        System.out.println("1.登陆");
        System.out.println("2.注册");
        String s = sc.next();
        switch (s){
            case "1"://登陆
                break;
            case "2"://注册
                    register();
                break;
            default:
                System.out.println("您输入的数据有误。");
                break;
        }
    }

    //注册功能
    private static void register() {
        Scanner sc=new Scanner(System.in);
        System.out.println("******注册页面******");
        System.out.println("请输入用户名：");
        String userName = sc.next();
        System.out.println("正在检测用户名是否合法。。。。");

        try {
            //把用户名发送给服务端
            op.write(userName.getBytes());

            Thread.sleep(2000);

            //接收服务端返回的数据
            int len = in.read(buf);


            //把接受的字节数组转化为字符数组---验证码
            String msg=new String(buf,0,len);
            System.out.println(msg);

            String code = msg.substring(msg.length()-4);
            System.out.println("*************************");


            //int code=Integer.parseInt(msg1);

            while (true) {
                System.out.println("请输入密码：");
                String password = sc.next();
                System.out.println("请确认密码");
                String OKpassword = sc.next();
                if(password.equals(OKpassword)){

                    while (true) {
                        System.out.println("请输入验证码：");
                        String userCode = sc.next();

                        if(userCode.equals(code)){
                            System.out.println("注册中，请稍等...");

                            //把用户名，密码，验证码发送给客户端
                            String data=userName+","+password+","+code;
                            op.write(data.getBytes());

                            //接收服务端信息
                            Thread.sleep(2000);

                            int len2 = in.read(buf);
                            String msg1=new String(buf,0,len2);
                            System.out.println(msg1);

                            break;

                        }else {
                            System.out.println("验证码输入错误，请重新输入！");
                        }
                    }
                    break;
                }else {
                    System.out.println("两次密码输入不相同，请重新输入！");
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }




    }
}
